<template>
	<view class="tender-detail">
		<view class="detail-card">
			<view class="card-title">
				项目信息
			</view>
			<view class="card-content">
				<view class="card-content-row">
					<view class="row-left">
						项目编号：
					</view>
					<view class="row-right">
						{{detailModel.project_code}}
					</view>
				</view>
			</view>
			<view class="card-content">
				<view class="card-content-row">
					<view class="row-left">
						项目名称：
					</view>
					<view class="row-right">
						{{detailModel.project_name}}
					</view>
				</view>
			</view>
			<view class="card-content">
				<view class="card-content-row">
					<view class="row-left" style="color:#B8212B;margin-top: 10px;">
						<!-- {{progressToText(progress,1)}} -->
					</view>

				</view>
			</view>
			<view class="card-content" v-if="progress=='资格审查'&&!isReview">
				<view class="card-content-row">
					<view class="row-left" style="color:#B8212B;">
						报名截止时间：
					</view>
					<view class="row-right" style="color:#B8212B;">
						{{detailModel.regist_end_date}}
					</view>
				</view>
			</view>
			<view class="card-content" v-if="detailModel.state=='未中标'">
				<view class="card-content-row">
					<view class="row-left" style="color:#B8212B;">
						审核时间：
					</view>
					<view class="row-right" style="color:#B8212B;">
						{{detailModel.reviewer_date}}
					</view>
				</view>
			</view>
			<view class="card-content" v-if="progress=='审查不通过'">
				<view class="card-content-row">
					<view class="row-left" style="color:#B8212B;">
						审核时间：
					</view>
					<view class="row-right" style="color:#B8212B;">
						{{detailModel.reviewer_date}}
					</view>
				</view>
			</view>

			<view class="card-content" v-if="progress=='开标评审'&&isAppraise">
				<view class="card-content-row">
					<view class="row-left" style="color:#B8212B;">
						中标时间：
					</view>
					<view class="row-right" style="color:#B8212B;">
						{{detailModel.win_date}}
					</view>
				</view>
			</view>
		</view>

		<view class="detail-card">
			<view class="card-title">
				当前项目进度
			</view>
			<view class="card-content" v-if="progress=='项目报名'">
				<view class="card-content-row">
					<view class="row-left" style="color:#B8212B;">
						项目报名
					</view>
					<view class="row-right" style="color:#B8212B;">
						已报名
					</view>
				</view>
			</view>
			<!-- 		<view class="card-content" v-if="progress=='项目报名'">
				<view class="card-content-row">
					<view class="row-left">
						报名时间
					</view>
					<view class="row-right">
						2020-12-10 12：30
					</view>
				</view>
			</view> -->
			<view class="card-content" v-if="progress=='审查不通过'">
				<view class="card-content-row">
					<view class="row-left" style="color:#B8212B;">
						资格审查:审查不通过
					</view>
				</view>
			</view>
			<view class="card-content" v-if="progress=='资格审查'&&!isReview">
				<view class="card-content-row">
					<view class="row-left" style="color:#B8212B;">
						资格审查:审核中
					</view>
				</view>
			</view>
			<view class="card-content" v-if="progress=='上传投标文件'">
				<view class="card-content-row">
					<view class="row-left" style="color:#B8212B;">
						资格审查:已通过
					</view>
				</view>
			</view>


			<view class="card-content" v-if="detailModel.state=='未中标'">
				<view class="card-content-row">
					<view class="row-left" style="color:#B8212B;">
						未中标
					</view>
				</view>
			</view>


			<view class="card-content" v-if="progress=='上传投标文件'">
				<view class="card-content-row">
					<view class="row-left">
						审核时间：{{detailModel.reviewer_date}}
					</view>
				</view>
			</view>
			<!-- 			<view class="card-content" v-if="detailModel.state=='未中标'">
				<view class="card-content-row">
					<view class="row-left">
						审核时间：{{detailModel.reviewer_date}}
					</view>
				</view>
			</view> -->


			<view class="card-content" v-if="progress=='开标评审'">
				<view class="card-content-row">
					<view class="row-left" style="color:#B8212B;">
						开标评审：{{isAppraise?'中标':'评审中'}}
					</view>
				</view>
			</view>
		</view>

		<view class="detail-card" v-if="progress=='上传投标文件'">
			<view class="card-title">
				上传标书
			</view>
			<view class="card-content">
				<view class="card-content-row" v-for="(item,index) in detailModel.attachment">
					<view class="row-left" style="color:#B8212B;" @click="previewFile(item)">
						{{item.name}}
					</view>
					<view class="row-right-icon" @click="deleteFile(item,index)">
						<image class="delete" src="../../../static/image/delete-icon.png" mode=""></image>
					</view>

				</view>
			</view>

			<view class="add-file" @click="chooseFile">
				<image src="../../../static/image/add-icon.png" mode=""></image>
				<text>继续添加</text>
			</view>
		</view>



		<view class="detail-card" v-if="progress=='开标评审'&&isAppraise">
			<view class="card-title">
				中标内容
			</view>
			<view class="card-content">
				<rich-text :nodes="detailModel.win_html"></rich-text>
			</view>
		</view>

		<view class="detail-card" v-if="progress=='开标评审'&&isAppraise">
			<view class="card-title">
				中标文件
			</view>
			<view class="card-content">
				<view class="card-content-row" v-for="(item,index) in detailModel.win_attachment">
					<a @click="previewFile(item)">{{item.name}}</a>
				</view>
			</view>
		</view>

		<view class="upload" @click="uploadFile()" v-if="progress=='上传投标文件'">
			确认上传
		</view>
	</view>
</template>

<script>
	import {
		getTenderDetail,
		deleteFile,
		changeState
	} from '@/static/js/api/tender.js';
	import {
		getUnusualData
	} from '@/static/js/api/bidding.js';
	import request from '@/static/js/request.js'
	export default {
		data() {
			return {
				//项目报名，资格审查，开标评审
				progress: '',
				isReview: false, //审核中
				isAppraise: false, //评审
				id: '',
				detailModel: {},
				isTongguo1: false
			}
		},
		onLoad(option) {
			this.id = option.id;
			this.getTenderDetail();
		},
		methods: {
			getTenderDetail() {
				getTenderDetail(this.id).then(res => {
					this.detailModel = res.data[0];
					this.progress = this.detailModel.state;

					if (this.progress == '资格审查') {
						this.isReview = false;
					} else if (this.progress == '上传投标文件') {

					} else if (this.progress == '中标') {
						this.progress = '开标评审';
						this.isAppraise = true
					}
				})
			},
			// progressToText(progress, num) {
			// 	if (progress == '项目报名') {
			// 		return `已有${num}个班组报名`
			// 	} else if (progress == '资格审查') {
			// 		return `已有${num}个班组审核通过`

			// 	} else if (progress == '开标评审') {
			// 		return `已有${num}个班组参与投标`
			// 	}
			// },
			//获取中标公告信息
			getUnusualData() {
				getUnusualData(this.biddingDetail.unusual2_id).then(res => {
					this.unusual2Data = res.data[0];
				})
			},
			chooseFile() {
				let userInfo = JSON.parse(uni.getStorageSync('userInfo'));
				let self = this;
				uni.showActionSheet({
					itemList: ['上传文件', '上传图片'],
					success: function(res) {
						// console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
						if (res.tapIndex == 0) {
							wx.chooseMessageFile({
								count: 1,
								type: 'file',
								success(res) {
									// var filename = res.tempFiles[0].name;
									// console.log(res)
									self.detailModel.attachment.push({
										name: res.tempFiles[0].name,
										url: res.tempFiles[0].path
									})
								}
							})
						} else {
							uni.chooseImage({
								count: 1,
								success(res) {
									console.log(res);
									self.detailModel.attachment.push({
										name: '附件图片' + (self.detailModel.attachment.length + 1),
										url: res.tempFiles[0].path
									})
									console.log(self.detailModel.attachment)

								}
							})
						}
					},
					fail: function(res) {
						console.log(res.errMsg);
					}
				})
			},
			async uploadFile() {
				uni.showLoading({
					title: '正在上传'
				})
				let userInfo = JSON.parse(uni.getStorageSync('userInfo'));
				let self = this;
				for (let i = 0; i < this.detailModel.attachment.length; i++) {
					let item = this.detailModel.attachment[i]
					if (!item.checksum) {
						await self.uploadFile2(item.url)
					}
				}
				// this.detailModel.attachment.forEach(item => {

				// })

				self.changeState();

			},
			uploadFile2(filePath) {
				let userInfo = JSON.parse(uni.getStorageSync('userInfo'));
				let self = this;
				uni.uploadFile({
					url: request.getBaseUrl() + '/api/v1/upload_img/9', // 后台服务URL
					filePath: filePath, // 微信返回的临时图片地址
					name: 'upload_files',
					header: {
						"Content-Type": "multipart/form-data"
					},
					formData: { // 其他参数
						access_token: userInfo.access_token,
						model: 'jhj.bidding.process', //当前对象（表名）
						uid: userInfo.uid, //当前用户 uid，      
						partner_id: userInfo.partner_id, //当前用户partner_id，
						res_id: self.id
					},

				});
			},
			//修改状态
			changeState() {
				changeState(this.id, '开标评审').then(res => {
					uni.navigateBack();
					uni.showToast({
						title: '上传成功'
					})
					console.log(res)
				}).catch(e => {
					console.log(e)
				})
			},

			previewFile(item) {
				if (this.isPicture(item.url)) {
					uni.previewImage({
						urls: [request.getBaseUrl() + item.url]
					})
				} else {
					//#ifdef MP-WEIXIN
					uni.downloadFile({
						url: request.getBaseUrl() + item.url,
						success: function(res) {
							var filePath = res.tempFilePath;
							uni.openDocument({
								filePath: filePath,
								showMenu: true, // 允许出现分享功能
								success: function(res) {
									console.log('打开文档成功');
								}
							});
						}
					});
					//#endif
				}

			},
			deleteFile(item, index) {
				let self = this;
				let userInfo = JSON.parse(uni.getStorageSync('userInfo'));
				uni.showModal({
					title: '警告',
					content: '是否删除此附件',
					success() {
						if (item.checksum) {
							deleteFile(item.checksum, userInfo.uid).then(res => {
								uni.showToast({
									title: '删除成功'
								})
								self.getTenderDetail();
							})
						} else {
							self.detailModel.attachment.splice(index, 1)
						}

					},
					fail() {

					}
				})
			},
			isPicture(str) {
				//判断是否是图片 - strFilter必须是小写列举

				return [
					'png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'svg',
				].indexOf(str.toLowerCase()) !== -1;

				// var strFilter = ".jpeg|.gif|.jpg|.png|.bmp|.pic|.svg|"
				// if (str.indexOf(".") > -1) {
				// 	var p = str.lastIndexOf(".");
				// 	var strPostfix = str.substring(p, str.length) + '|';
				// 	strPostfix = strPostfix.toLowerCase();
				// 	if (strFilter.indexOf(strPostfix) > -1) {
				// 		return true;
				// 	}
				// }
				// return false;
			}
		}
	}
</script>

<style lang="less" scoped>
	.tender-detail {
		background: #F4F5F9;
	}

	.detail-card {
		padding: 30rpx;
		background: #fff;
		margin-bottom: 20rpx;

		.card-title {
			font-size: 36rpx;
			font-weight: 500;
			color: #333333;
			margin-bottom: 24rpx;
		}

		.add-file {
			border-radius: 6rpx;
			border: 2rpx dashed #BDBDBD;
			display: flex;
			align-items: center;
			justify-content: center;
			height: 80rpx;
			margin-top: 20rpx;

			image {
				width: 36rpx;
				height: 36rpx;
			}

			text {
				font-size: 28rpx;
				font-weight: 500;
				color: #333333;
				margin-left: 22rpx;
			}
		}

		.card-content {
			.card-content-row {
				display: flex;
				font-size: 28rpx;
				font-weight: 500;
				color: #666666;
				line-height: 40rpx;
				margin-bottom: 6rpx;

				.row-left {
					overflow: hidden;
					text-overflow: ellipsis;
				}

				.row-right {
					flex: 1;
				}

				.row-right-icon {
					flex: 1;
					text-align: right;

					image {
						width: 48rpx;
						height: 48rpx;
					}
				}

				a {
					font-size: 28rpx;
					font-weight: 500;
					color: #B8212B;

				}
			}
		}
	}


	.upload {
		width: 680rpx;
		height: 80rpx;
		background: #B8212B;
		border-radius: 8rpx;

		font-size: 32rpx;
		font-weight: 500;
		color: #FFFFFF;
		margin: 0 auto;
		line-height: 80rpx;
		text-align: center;
	}
</style>
